package cn.net.sinodata.threadpool.thread;

import cn.net.sinodata.common.core.utils.SpringUtils;
import cn.net.sinodata.threadpool.base.AbstractThreadPool;
import cn.net.sinodata.threadpool.processor.Test01Processor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Description: Test01线程池类
 *
 * @author: yinliangyun
 * @date: 2021-5-19
 */
@Slf4j
public class Test01ThreadPool extends AbstractThreadPool {

	public void run() {
		try {
			Thread.sleep(20 * 60 * 1000);
		} catch (InterruptedException e) {
			log.info("队列线程池终止,队列名称:" + getThreadPoolName());
			return;
		}

		while (isWorkState()) {

			if (this.isBusy()) {
				try {
					Thread.sleep(getSleepTime() * 1000);
				} catch (InterruptedException e) {
					break;
				}
				continue;
			}

			if (!isRunTime()) {
				log.info("不在执行时间范围内，开始时间：" + this.getRunStartTime() + ",结束时间：" + this.getRunEndTime());
				try {
					Thread.sleep(getSleepTime() * 1000);
				} catch (InterruptedException e) {
					break;
				}
				continue;
			}

			try {
				Thread.sleep(getSleepTime() * 10000);
				log.info("sleep 10000");
			} catch (InterruptedException e) {
				log.error("InterruptedException", e);
			}

			Test01Processor processor = (Test01Processor) SpringUtils.getBean("test01Processor");
				Runnable t = new Runnable() {
					public void run() {
						try {
							long processstart = System.currentTimeMillis();
							processor.processTask(getThreadPoolName() + "_" + processstart);
							long processsend = System.currentTimeMillis();
							log.info("耗时：" + (processsend - processstart) + "ms ");
						} catch (Throwable e) {
							log.error("失败", e);
						}
					}
				};
				// 将任务提交至线程池处理
				this.execute(t);
		}

		log.info("队列线程池终止,队列名称:" + getThreadPoolName());
	}

}
